package kr.co.boogui.dao;

import java.util.List;

import kr.co.boogui.model.MemberVO;
import kr.co.boogui.model.ParamVO;
import mybatis3.helper.MVCSqlSessionFactory;

import org.apache.ibatis.session.SqlSession;

public class MemberDAO {
	public static MemberVO login(MemberVO m) {
		MemberVO result = null;
		SqlSession sess = null;
		try {
			sess = MVCSqlSessionFactory.openSession();
			result = sess.selectOne("MemberMapper.loginSQL", m);
		} finally {
			sess.close();
		}
		return result;
	}
	
	public static boolean changePwd(MemberVO m) {
		boolean result = false;
		SqlSession sess = null;
		try {
			sess = MVCSqlSessionFactory.openSession();
			if(sess.update("MemberMapper.updatePwdSQL", m)>0) {
				result = true;
			}
			sess.commit();
		} finally {
			sess.close();
		}
		return result;
	}
	
	public static String chkPwd(String pwd) {
		String result = "";
		SqlSession sess = null;
		try {
			sess = MVCSqlSessionFactory.openSession();
			result = sess.selectOne("MemberMapper.chkPwdSQL", pwd);
		} finally {
			sess.close();
		}
		return result;
	}
	
	public static List<MemberVO> selectNewMember() {
		List<MemberVO> result = null;
		SqlSession sess = null;
		try {
			sess = MVCSqlSessionFactory.openSession();
			result = sess.selectList("MemberMapper.selectNewMemberSQL");
		} finally {
			sess.close();
		}
		return result;
	}
	public static boolean idcheck(MemberVO m) {
		boolean result = false;
		int gab;
		SqlSession sess = null;
		try {
			sess = MVCSqlSessionFactory.openSession();
			gab =sess.selectOne("MemberMapper.idcheckSQL", m);
			if(gab != 0)
				result = true;
		} finally {
			sess.close();
		}
		return result;
	}
	
	public static int selectTodayRegiMember() {
		int result = 0;		
		SqlSession sess = null;
		try {
			sess = MVCSqlSessionFactory.openSession();
			result = sess.selectOne("MemberMapper.selectTodayRegiMemberSQL");
		} finally {
			sess.close();
		}
		return result;
	}
	
	public static List<MemberVO> selectMemberList(String order, int startno, String ftype, String fkey) {
		int perpage = 15;
		List<MemberVO> result = null;
		SqlSession sess = null;
		ParamVO pvo = new ParamVO();
		pvo.setOrder(order);
		pvo.setStartno(startno);
		pvo.setPerpage(perpage);
		pvo.setFtype(ftype);
		pvo.setFkey(fkey);
		try {
			sess = MVCSqlSessionFactory.openSession();
			result = sess.selectList("MemberMapper.selectAllMemberSQL", pvo);
		} finally {
			sess.close();
		}
		return result;
	}
	
	public static int selectAllTotalMember() {
		int result = 0;
		SqlSession sess = null;
		try {
			sess = MVCSqlSessionFactory.openSession();
			result = sess.selectOne("MemberMapper.selectAllTotalMemberSQL");
		} finally {
			sess.close();
		}
		return result;
	}
	public static boolean pwdcheck(MemberVO m) {
		boolean result = false;
		int gab;
		SqlSession sess = null;
		try {
			sess = MVCSqlSessionFactory.openSession();
			gab =sess.selectOne("MemberMapper.pwdcheckSQL", m);
			if(gab != 0)
				result = true;
		} finally {
			sess.close();
		}
		return result;
	}	
	
	public static int selectTotalMember(String ftype, String fkey) {
		int result = -1;
		ParamVO pvo = new ParamVO();
		pvo.setFtype(ftype);
		pvo.setFkey(fkey);
		SqlSession sess = MVCSqlSessionFactory.openSession();
		try {
			result = sess.selectOne("MemberMapper.selectTotalMemberSQL", pvo);
		} finally {
			sess.close();
		}		
		return result;
	}
	
	public static boolean updateAdmMember(MemberVO m) {
		boolean result = false;
		SqlSession sess = null;
		try {
			sess = MVCSqlSessionFactory.openSession();
			if(sess.update("MemberMapper.updateAdmMemberSQL", m)>0) {
				result = true;
			}
			sess.commit();
		} finally {
			sess.close();
		}
		return result;
	}
	public static MemberVO selectAdmMember(MemberVO m) {
		MemberVO result = null;
		SqlSession sess = MVCSqlSessionFactory.openSession();
		try {
			result = sess.selectOne("MemberMapper.selectAdmMemberSQL", m);
		} finally {
			sess.close();
		}		
		return result;
	}
	
	public static boolean insertMember(MemberVO m) {
		boolean result = false;
		SqlSession sess = null;
		System.out.println(m);
		try {
			sess = MVCSqlSessionFactory.openSession();
			if (sess.insert("MemberMapper.insertmemberSQL", m) > 0) {
				result = true;
			}
			sess.commit(); // insert나 update 시 반드시 작성해야함
		} finally {
			sess.close();
		}

		return result;
	}
	public static MemberVO readMember(MemberVO m) {
		MemberVO result = new MemberVO();
		SqlSession sess = null;
		
		try {
			sess = MVCSqlSessionFactory.openSession();
			result = sess.selectOne("MemberMapper.readMemerSQL", m);	
			System.out.println(result);
		} finally {
			sess.close();
		}
		return result;
	}
	public static boolean deleteMember(MemberVO m) {
		boolean result = false;
		SqlSession sess = null;
		try {
			sess = MVCSqlSessionFactory.openSession();
			if(sess.delete("MemberMapper.deleteMemberSQL", m)>0) {
				result = true;
			}
			sess.commit();
		} finally {
			sess.close();
		}
		return result;
	}
	public static boolean updatemodifyMember(MemberVO m) {
		boolean result = false;
		SqlSession sess = null;
		try {
			sess = MVCSqlSessionFactory.openSession();
			if(sess.update("MemberMapper.updatemodifyMemberSQL", m)>0) {
				result = true;
			}
			sess.commit();
		} finally {
			sess.close();
		}
		return result;
	}
}
